﻿package com.neriksworkshop.colourLoversDemos.colourExplore.view 
{
	import com.neriksworkshop.lib.colourLovers.items.Color;
	import com.neriksworkshop.lib.colourLovers.items.Palette;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author ...
	 */
	public class MainView extends Sprite
	{
		
		public static const SQUARE_SIZE:Number = 200;
		
		private var squares:/*MovieClip*/Array = new Array();
		private var squaresContainer:Sprite = new Sprite();
		
		public function MainView() 
		{
			
			buildSquares();

			mouseChildren = false;
			
			addChild(squaresContainer);
		}
		
		public function showPalette(palette:Palette, clickedIndex:int = -1, newPaletteIndex:int = -1):void
		{		

			trace(clickedIndex, newPaletteIndex)
			if (clickedIndex != -1)
			{
				mouseChildren = false;
				var oldMovesFrom:Number = (newPaletteIndex - clickedIndex) * SQUARE_SIZE;
				TweenLite.to(squaresContainer, .3, { x: oldMovesFrom, onComplete: showPalette2, onCompleteParams: [palette, clickedIndex, newPaletteIndex] } );
				for (var i:int = 0; i < 5; i++) 
				{
					if (i != clickedIndex) TweenLite.to(squares[i], .3, { alpha:0})
				}
			}
			else
			{
				drawNewPalette(palette);
			}
			

		}
		

		public function showPalette2(palette:Palette, clickedIndex:int, newPaletteIndex:int):void
		{		
			squaresContainer.x = 0;
			
			destroySquares();
			buildSquares();
					
			drawNewPalette(palette)

		}
		
		
		public function drawNewPalette(palette:Palette):void
		{					
			
			for (var i:int = 0; i < 5; i++) 
			{
				TweenLite.from(squares[i], .2, { alpha:0 } );
				squares[i].graphics.clear();
				squares[i].graphics.beginFill(palette.colorEntities[i].hex);
				squares[i].graphics.drawRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
				
			}
			
			mouseChildren = true;
			
			TweenLite.to(squaresContainer, .02, { x: 0 } );
		}
		
		public function setAvailability(index:int, numPalettes:int):void
		{
			squares[index].mouseEnabled = true;
			squares[index].buttonMode = true;
			squares[index].waiting.visible = true;
			squares[index].waiting.text = String(numPalettes);
			
		}
		
		
		
		private function buildSquares():void
		{
						
			var squareX:Number = 0;
			
			for (var i:int = 0; i < 5; i++) 
			{
				var square:MovieClip = new MovieClip();
				square.x = squareX;
				square.index = i;
				//square.addEventListener(MouseEvent.CLICK, onSquareClick);
				squareX += SQUARE_SIZE;
				squares.push(square);
				squaresContainer.addChild(square);	
				
				var waiting:TextField = new TextField();
				square.mouseEnabled = false;
				square.buttonMode = false;
				square.addChild(waiting)
				square.waiting = waiting;
			}
			
		}
		
		private function destroySquares():void
		{
			for (var i:int = 0; i < 5; i++) 
			{
				var square:MovieClip = squares[i];
				squaresContainer.removeChild(square);				
			}
			
			squares = [];
		}
		
		
	}
	
}